﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace m.Website.Services
{
    public partial class ResizeImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string kq = "";
            string url = Request.QueryString["url"] == null ? "" : Request.QueryString["url"].ToString();
            string width = Request.QueryString["width"] == null ? "" : Request.QueryString["width"].ToString();
            kq = resizeanh(url, "/Upload/Web/thumbimages/", int.Parse(width));
            Response.ContentType = "image/jpeg";
            Response.WriteFile(kq);
        }
        private string resizeanh(string filename, string thumuc, int thumbWidth)
        {
            string linkFileRe = "N/A";
            string pathAvatars = Server.MapPath(thumuc);
            //try
            //{
            int thumbHeight;
            System.Drawing.Image oImg = System.Drawing.Image.FromFile(Server.MapPath(filename));//Bitmap.FromFile(Server.MapPath(filename));
            thumbHeight = (int)(oImg.Height * ((float)thumbWidth / (float)oImg.Width));
            System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);


            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighSpeed;
            oGraphic.SmoothingMode = SmoothingMode.HighSpeed;
            oGraphic.InterpolationMode = InterpolationMode.High;

            Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
            oGraphic.DrawImage(oImg, oRectangle);
            string nameAdd = "_Thumb_" + thumbWidth.ToString() + "_";
            string filesource = Server.MapPath(thumuc) + nameAdd + Path.GetFileName(filename);

            if (File.Exists(filesource))
            { }
            else
            {
                oThumbNail.Save(filesource, ImageFormat.Jpeg);
                oImg.Dispose();
            }
            linkFileRe = thumuc + nameAdd + Path.GetFileName(filename);

            return linkFileRe;

        }
    }
}